/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.view;

import java.util.Map; 
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.IEditorInput;
import org.mags.remas.view.commons.ImageProvider;
import org.mags.remas.view.commons.RelationshipEditorBase;
import org.mags.remas.view.widgets.ListViewer;
import org.mags.remas.control.ListEditorInput;
import org.mags.remas.control.commons.ICrudActions;

/**
 * 
 * @author Mario Gaitan
 *
 */
public class ListEditor extends RelationshipEditorBase {
	public static final String ID = "org.mags.remas.view.listEditor";
	private ListViewer listViewer;
	private ICrudActions crudActions;
	private String[] properties;
	private String[] titles;
	private String[] labels;
	private Map<String, Object[]> comboValues;
	
	public Map<String, Object[]> getComboValues() {
		return comboValues;
	}
	public String[] getLabels() {
		return labels;
	}
	public String[] getTitles() {
		return titles;
	}
	public String[] getProperties() {
		return properties;
	}
	public ICrudActions getCrudActions() {
		return crudActions;
	}
	public void setCrudActions(ICrudActions crudActions) {
		this.crudActions = crudActions;
	}
	public void setListViewer(ListViewer listViewer) {
		this.listViewer = listViewer;
	}
	public ListViewer getListViewer() {
		return listViewer;
	}
	public Image getElementImage() {
		return ImageProvider.getCommonImage("value.png");
	}
	protected void createListViewer(Composite parent) {
		listViewer = new ListViewer(parent, SWT.NONE, properties, titles, true);
		listViewer.setLabels(labels);
		listViewer.setComboValues(comboValues);
		listViewer.setElementImage(getElementImage());
		listViewer.setCrudActions(crudActions);
		listViewer.setInput(crudActions);
		listViewer.getControl().setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));		
	}
	@Override
	public void createPartControl(Composite parent) {
		parent.setLayout(new GridLayout(1, true));
		createToolBar(parent);
		createListViewer(parent);
	}
	protected void createDeleteButton(ToolBar toolBar) {
		ToolItem deleteButton = new ToolItem(toolBar, SWT.NONE);		
		deleteButton.setText("delete");
		deleteButton.setImage(ImageProvider.getCommonImage("delete.png"));
		deleteButton.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			public void widgetSelected(SelectionEvent e) {
				listViewer.getDeleteAction().run();
			}
		});
	}
	protected void createEditButton(ToolBar toolBar) {
		ToolItem editButton = new ToolItem(toolBar, SWT.NONE);		
		editButton.setText("edit");
		editButton.setImage(ImageProvider.getCommonImage("edit.png"));
		editButton.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			public void widgetSelected(SelectionEvent e) {
				listViewer.getEditAction().run();
			}
		});
	}
	protected void createNewButton(ToolBar toolBar) {
		ToolItem newButton = new ToolItem(toolBar, SWT.NONE);		
		newButton.setText("new");
		newButton.setImage(ImageProvider.getCommonImage("new.png"));
		newButton.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			public void widgetSelected(SelectionEvent e) {
				listViewer.getCreateAction().run();
			}
		});
	}
	protected void createRefreshButton(ToolBar toolBar) {
		ToolItem refreshButton = new ToolItem(toolBar, SWT.NONE);		
		refreshButton.setText("refresh");
		refreshButton.setImage(ImageProvider.getCommonImage("refresh.png"));
		refreshButton.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			public void widgetSelected(SelectionEvent e) {
				listViewer.getRefreshAction().run();
			}
		});
	}
	protected void createToolBar(Composite parent) {
		ToolBar toolBar = new ToolBar(parent, SWT.FLAT);
		GridData gridData = new GridData();
		gridData.horizontalAlignment = SWT.RIGHT;
		gridData.grabExcessHorizontalSpace = true;		
		toolBar.setLayoutData(gridData);

		createNewButton(toolBar);
		createEditButton(toolBar);
		createDeleteButton(toolBar);
		createRefreshButton(toolBar);
	}
	@Override
	public boolean ignoreException(Exception e) {
		return false;
	}
	@Override
	public void init(IEditorInput input) {
		properties = ((ListEditorInput)input).getProperties();
		titles = ((ListEditorInput)input).getTitles();
		labels = ((ListEditorInput)input).getLabels();
		comboValues = ((ListEditorInput)input).getComboValues();
	}
	@Override
	public void setFocus() {
		listViewer.getControl().setFocus();
	}
}
